package com.mmall.service.impl;

import com.mmall.common.ServerResponse;
import com.mmall.dao.UserMapper;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import com.mmall.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/5/23 0023.
 */

@Service("iUserService")
//把service注入到Controller上,供Controller调用
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;


    /*
    登录
       业务逻辑：
     */
    @Override
    public ServerResponse<User> login(String username, String password) {
        int resultCount = userMapper.checkUsername(username);
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("用户名不存在");
        }

        //登陆密码MD5加密
        String md5password = MD5Util.MD5EncodeUtf8(password);

        User user = userMapper.selectLogin(username, md5password);
        if (user == null) {
            return ServerResponse.createByErrorMessage("密码错误！");
        }
        /*
        为了安全考虑，把密码设置为空值
         */
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccessMessageAndData("登录成功", user);


    }


}
